//
//  ViewController.m
//  MyButton
//
//  Created by IOS_wt on 15/12/3.
//  Copyright © 2015年 wangteng. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    UIButton *button;
    NSLayoutConstraint* layout;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    button=[[UIButton alloc]init];
    [button setTitle:@"ok15644" forState:UIControlStateNormal];
    button.translatesAutoresizingMaskIntoConstraints=NO;
    [button setBackgroundColor:[UIColor greenColor]];
    [button sizeToFit];
    [button addTarget:self action:@selector(clicked) forControlEvents: UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    //    NSArray *constraints1=[NSLayoutConstraint constraintsWithVisualFormat:@"H:[button(40)]"options:0 metrics:nil views:NSDictionaryOfVariableBindings(button)];
    //
    //    NSArray *constraints2=[NSLayoutConstraint constraintsWithVisualFormat:@"V:[button(40)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button)];
    //    [self.view addConstraints:constraints1];
    //    [self.view addConstraints:constraints2];
    
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterX relatedBy: NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    
    self->layout=[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeBottom relatedBy: NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
    
    [self.view addConstraint:self->layout];

    
}
-(void)clicked{
    
    [self.view removeConstraint:self->layout];
    
    
    [UIView animateWithDuration:3 animations:^{
        
        [self.view addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeBottom relatedBy: NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
        
        [button layoutIfNeeded];//刷新约束改变
        [button layoutSubviews];
        
    }];
    //[button addTarget:self action:@selector(aa) forControlEvents:  UIControlEventTouchUpInside ];
    
}
-(void)aa{
    
    [UIView animateWithDuration:2.5 animations:^{
        
        NSArray *constraints3=[NSLayoutConstraint constraintsWithVisualFormat:@"H:[button(40)]"options:0 metrics:nil views:NSDictionaryOfVariableBindings(button)];
        
        NSArray *constraints4=[NSLayoutConstraint constraintsWithVisualFormat:@"V:[button(40)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button)];
        
        [self.view addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeTop relatedBy: NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
        
        [self.view addConstraints:constraints3];
        [self.view addConstraints:constraints4];
        
        [button layoutIfNeeded];//刷新约束改变
        
    }];
    //button.center = CGPointMake(_center.x, _center.y);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
